package com.lhd_gm.scheduledtasksystem.utils;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;

public class DynamicBeanRegistrar {

    public static void registerJobBean(ApplicationContext context, String beanName, Class<?> clazz) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();

        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClassName(clazz.getName());
        beanDefinition.setScope("prototype"); // 建议使用原型模式
        beanDefinition.setLazyInit(true);

        registry.registerBeanDefinition(beanName, beanDefinition);
    }
}
